package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.common.ResultCode;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.entity.Account;
import com.example.springboot.entity.Admin;
import com.example.springboot.entity.User;
import com.example.springboot.service.AdminService;
import com.example.springboot.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/account")
public class AccountController {
    @Resource
    AdminService adminService;

    @Resource
    UserService userService;



    /**
     * 注册接口
     */

    @PostMapping("/register")
    public Result accountRegister(@RequestBody Account account){
        Integer role = account.getRole();
        Account login = new Account(); // 定义一个Account类的login,用于返回给前端
        if ( 2 == role){ //这里只有用户注册
            User user = new User();
            BeanUtils.copyProperties(account,user);
            login = userService.userRegister(user);
        }
        return Result.success(login);
    }


    /**
     * 用户登录接口
     */

    @PostMapping("/login")
    public Result acountLogin(@RequestBody Account account){
        Integer role = account.getRole();
        Account login = new Account(); // 定义一个Account类的login,用于返回给前端
        if ( 1 == role){
            Admin admin = new Admin(); //创建一个管理员类admin
            BeanUtils.copyProperties(account,admin); //把父类的属性拷贝到admin
            login = adminService.adminLogin(admin); //调用服务层的登录逻辑
        }
        if ( 2 == role){
            User user = new User();
            BeanUtils.copyProperties(account,user);
            login = userService.userlogin(user);
        }

        //        生成token
        String token = JwtTokenUtils.genToken(login.getId() + "-" + login.getRole(), login.getPassword());
        //        创建一个键值对map集合，把token和user塞进去，返回给前端
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", login);
        return Result.success(map); //装好数据后，把map结合返回给前端

    }

    /**
     * 描述：获取当前登录用户信息
     */
    @GetMapping("/getUser")
    public Result getAccountInfo() {
        Account loginUser = JwtTokenUtils.getCurrentUser();
        if (loginUser == null) {
            return Result.success(new Object());
        }
        Integer role = loginUser.getRole();
        if (1 == role) {
            return Result.success(adminService.findById(loginUser.getId()));
        }
        if (2 == role) {
            return Result.success(userService.findById(loginUser.getId()));
        }

        return Result.success(new Object());
    }
    /**
     * 描述：更新密码接口
     */
    @Transactional
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account info) {
        //        1.获取当前登录用户 判断是否登录
        Account account = JwtTokenUtils.getCurrentUser();
        if (account == null) {
            return Result.error(ResultCode.USER_NOT_EXIST_ERROR.msg);
        }
        //        2。旧密码校验：获取前端传过来的要修改用户的旧密码
        String oldPassword = info.getPassword();
        if (!oldPassword.equals(account.getPassword())) {
            return Result.error(ResultCode.PARAM_PASSWORD_ERROR.msg);
        }
        //        3.重置密码：旧密码校验通过后，根据角色判断管理员还是用户，重置数据库用户的密码
        Integer role = account.getRole();
        if (1 == role) {
            Admin admin = new Admin();
            BeanUtils.copyProperties(account, admin);
            admin.setPassword(info.getNmm());
            adminService.adminEdit(admin);
        }
        if (2 == role) {
            User user = new User();
            BeanUtils.copyProperties(account, user);
            user.setPassword(info.getNmm());
            userService.userEdit(user);
        }

        return Result.success();
    }


}
